<?php 
class Ajax extends CI_Controller 
{
	public function check()
	{
		$this->load->model('M_bookings');
		$times = $this->M_bookings->getTimes();
		$booked = $this->M_bookings->getBookedTimes($this->input->post('stylist'), $this->input->post('adate'));
		$list = '<div class="input-block"><label>Select Time</label></div> <ul class="available-time">';
		
		if(!empty($booked)){
			$booked1 = $this->objectToArray($booked);
		
		}
		foreach($times as $t){
			if(isset($booked1)){
				if(in_array($t->aps_id, $booked1))
					$list = $list.'<li><span>'.date('g:i A', strtotime($t->aps_time_from)).'-'.date('g:i A', strtotime($t->aps_time_to)).'</span></li>';
				else
					$list = $list.'<li><a href="#" rel="'.$t->aps_id.'" class="s_time" >'.date('g:i A', strtotime($t->aps_time_from)).'-'.date('g:i A', strtotime($t->aps_time_to)).'</a></li>';
			}
			else
				$list = $list.'<li><a href="#" rel="'.$t->aps_id.'" class="s_time" >'.date('g:i A', strtotime($t->aps_time_from)).'-'.date('g:i A', strtotime($t->aps_time_to)).'</a></li>';
				
		}
		
		
		
		$list = $list.'</ul>';
		echo $list;
	
	}
	
	public function chat(){
		$this->load->model('M_chat');
		
		$booking_id = $this->input->post('booking_id');
		$message = $this->input->post('message');
		$chat_by = $this->input->post('chat_by');
		$array['last_chat_id'] = $this->M_chat->insert_chat($booking_id, $message, $chat_by);
		$array['msg'] = $message;
		$array['chat_by'] = $chat_by;
		header('Content-Type: application/json',true);
		echo json_encode($array);
		
	}
	
	public function get_chat(){
		$booking_id = $this->input->post('booking_id');
		$this->load->model('M_chat');
		$chat = $this->M_chat->get_chats($booking_id);
	
		
     	 header('Content-Type: application/json',true);
     	 echo json_encode($chat);
		
	}
	
	public function get_latest_chat(){
		$booking_id = $this->input->post('booking_id');	
		$chat_string_id = $this->input->post('chat_id_string');
		$chat_string_id = substr_replace($chat_string_id, '', -1);
		$this->load->model('M_chat');
		$latest_chats = $this->M_chat->getLatestChat($booking_id, $chat_string_id);
     	header('Content-Type: application/json',true);
     	echo json_encode($latest_chats);
	}
	
	private function objectToArray($object){
		$array = array();
		foreach($object as $o){
			$array[] = $o->time_id;	
		}
		return $array;
	}
}
?>